package cn.telling.sellercentre.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.common.util.TellingConstance;
import cn.telling.sellerCenter.vo.SellerBasePrice;
import cn.telling.sellerCenter.vo.SellerBasePriceVo;
import cn.telling.sellercentre.dao.ISellerBasePriceDao;
import cn.telling.utils.BaseUtil;
import cn.telling.utils.StringHelperTools;
/**
 * @Description: 基准价格处理Dao
 * @author liugeng
 *
 */

@Repository("sellerBasePriceDao")
public class SellerBasePriceDaoImpl implements ISellerBasePriceDao {
	private static Logger logger = Logger.getLogger(SellerBasePriceDaoImpl.class);
	@Autowired
	@Qualifier("film-template")
	private JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/**获取基准价格*/
	@Override
	public SellerBasePrice getSellerBasePrice(String supplyId, String productId) {
		String sql = "select supplyId, productId, baseprice from seller_basePrice where supplyId = ? and productId = ? and del_flag = 1 ";
		Object[] params = new Object[]{supplyId,productId};
		params = StringHelperTools.nullToString(params);
		
		logger.debug("获取基准价格 ：" + BaseUtil.logSQL(sql, params));
		List<SellerBasePrice> list = jdbcTemplate.query(sql, params, tempMapper);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	
	private ParameterizedRowMapper<SellerBasePrice> tempMapper = new ParameterizedRowMapper<SellerBasePrice>() {
		@Override
		public SellerBasePrice mapRow(ResultSet rs, int rowNum) throws SQLException {
			SellerBasePrice sellerBasePrice = new SellerBasePrice();
			sellerBasePrice.setSupplyId(rs.getBigDecimal("supplyId"));
			sellerBasePrice.setProductId(rs.getBigDecimal("productId"));
			sellerBasePrice.setBasePrice(rs.getBigDecimal("baseprice"));
			return sellerBasePrice;
		}
	};
	
	/**入库基准价格*/
	@Override
	public int insertSellerBsePrice(SellerBasePrice sellerBasePrice) {
		String sql = "insert into seller_basePrice(id, supplyId, productId, baseprice, add_date, add_user, update_date, update_user, del_flag) " +
					 " values(?,?,?,?,sysdate,?,sysdate,?,?)";
		Object[] params = new Object[]{sellerBasePrice.getId(),sellerBasePrice.getSupplyId(), 
						sellerBasePrice.getProductId(),sellerBasePrice.getBasePrice(),
						sellerBasePrice.getAdd_user(),sellerBasePrice.getUpdate_user(),
						TellingConstance.DEL_FLAG_1
					};
		params = StringHelperTools.nullToString(params);
		logger.debug("入库基准价格 ：" + BaseUtil.logSQL(sql, params));
		int count = jdbcTemplate.update(sql, params);
		return count;
	}
	
	/**更新基准价格*/
	@Override
	public int updateSellerBasePrice(BigDecimal basePrice,BigDecimal userId,String supplyId, String productId) {
		String sql = "update seller_basePrice set basePrice = ?, update_date = sysdate, update_user = ? where supplyId = ? and productId = ? ";
		Object[] params = new Object[]{basePrice, userId,supplyId,productId};
		params = StringHelperTools.nullToString(params);
		int count = jdbcTemplate.update(sql, params);
		logger.debug("更新基准价格 ：" + BaseUtil.logSQL(sql, params));
		return count;
	}
	
	/**批量更新基准价格*/
	@Override
	public int updateSellerBasePriceBatch(List<SellerBasePrice> sellerBasePriceList) {
		String sql = "update seller_basePrice set basePrice = ?, update_date = sysdate, update_user = ? where supplyId = ? and productId = ? ";
		List<Object[]> paramsList = new ArrayList<Object[]>();
		for (SellerBasePrice sellerBasePrice : sellerBasePriceList) {
			Object[] params = new Object[]{sellerBasePrice.getBasePrice(), sellerBasePrice.getAdd_user(),
											sellerBasePrice.getSupplyId(),sellerBasePrice.getProductId()
										};
			params = StringHelperTools.nullToString(params);
			
			paramsList.add(params);
		}
		logger.debug("批量更新基准价格 ：" + BaseUtil.logSQL(sql, new Object[]{}));
		
		int count[] = jdbcTemplate.batchUpdate(sql,paramsList);
		
		return count.length;
	}
	
	/**获得基准价格列表*/
	@Override
	public List<SellerBasePriceVo> getgetSellerBasePriceList(String supplyId) {
		String sql = "select pm.productId,pm.productName,pmd.modelName,sb.supplyId,sb.baseprice " +
					 " from seller_basePrice sb " +
					 " left join product_main pm on pm.productId = sb.productId " +
					 " left join product_model pmd on pmd.modelId = pm.productPattern " +
					 " where pm.saleType = '" + TellingConstance.SALETYPE_2 + "' and pm.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and pm.publish = '1' " +
					 " and sb.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and sb.supplyId = " + supplyId;
		logger.debug("获得基准价格列表 ：" + sql);
		List<SellerBasePriceVo> list = jdbcTemplate.query(sql, rowMapper);
		if(list != null){
			return list;
		}
		return null;
	}
	
	/**获得基准价格列表  非通讯市场*/
	@Override
	public List<SellerBasePriceVo> getSellerBasePriceList(String supplyId) {
		String sql = "select pm.productId,pm.productName,pmd.modelName,sb.supplyId,sb.baseprice " +
				 " from seller_basePrice sb " +
				 " left join product_main pm on pm.productId = sb.productId " +
				 " left join product_model pmd on pmd.modelId = pm.productPattern " +
				 " where pm.saleType = '" + TellingConstance.SALETYPE_1 + "' and pm.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and pm.publish = '1' " +
				 " and sb.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and sb.supplyId = " + supplyId;
		logger.debug("获得基准价格列表 ：" + sql);
		List<SellerBasePriceVo> list = jdbcTemplate.query(sql, rowMapper);
		if(list != null){
			return list;
		}
		return null;
	}
	
	private ParameterizedRowMapper<SellerBasePriceVo> rowMapper = new ParameterizedRowMapper<SellerBasePriceVo>() {
		@Override
		public SellerBasePriceVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			SellerBasePriceVo sellerBasePriceVo = new SellerBasePriceVo();
			sellerBasePriceVo.setProductId(rs.getBigDecimal("productId"));
			sellerBasePriceVo.setProductName(rs.getString("productName"));
			sellerBasePriceVo.setModelName(rs.getString("modelName"));
			sellerBasePriceVo.setSupplyId(rs.getBigDecimal("supplyId"));
			sellerBasePriceVo.setBasePrice(rs.getBigDecimal("baseprice"));
			
			return sellerBasePriceVo;
		}
	};

	@Override
	public List<SellerBasePriceVo> getgetSellerBasePriceListOne(String supplyId, String ProductfirstSortId,String productSortId) {
		// TODO Auto-generated method stub
		
		String sql = "select pm.productId,pm.productName,pmd.modelName,sb.supplyId,sb.baseprice " +
				 " from seller_basePrice sb " +
				 " left join product_main pm on pm.productId = sb.productId " +
				 " left join product_model pmd on pmd.modelId = pm.productPattern " +
				 " where pm.saleType = '" + TellingConstance.SALETYPE_2 + "' and pm.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and pm.publish = '1' " +
				 " and sb.del_flag = '" + TellingConstance.DEL_FLAG_1 + "' and sb.supplyId = " + supplyId;
		         if(!"".equals(productSortId)){
		    	   sql=sql+"and pm.sortid ="+productSortId;
		         }
		         else{
		          if(!"".equals(ProductfirstSortId)){
		        	sql=sql+"and pm.sortid in(select SORTID from product_sort where PARENTID=" + ProductfirstSortId+"and DEL_FLAG='1'" +")"; 		        	 
		          }
		         }
		         
	    logger.debug("获得基准价格列表 ：" + sql);
	    List<SellerBasePriceVo> list = jdbcTemplate.query(sql, rowMapper);
	    if(list != null){
		  return list;
	       }
	      return null;		
	  }
	
}